<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\Models\City;
use Illuminate\Http\Request;

/**
 * Class CityController
 * @package App\Http\Controllers\Manage
 */
class CityController extends ApiController
{
    /**
     * @var \App\Models\City
     */
    protected $city;

    /**
     * @var \Illuminate\Http\Request
     */
    protected $request;

    /**
     * CityController constructor.
     * @param \App\Models\City         $city
     * @param \Illuminate\Http\Request $request
     */
    public function __construct(City $city, Request $request)
    {
        parent::__construct();

        $this->city    = $city;
        $this->request = $request;
    }

    /**
     * 获取所有省份
     * @return \Illuminate\Http\JsonResponse
     */
    public function provinces()
    {
        return $this->response($this->city->provinces(['id', 'name']));
    }

    /**
     * 根据给定的省ID，获取省下的市
     * @return \Illuminate\Http\JsonResponse
     */
    public function cities()
    {
        $id = $this->request->input('province_id');

        return $this->response($this->city->cities($id, ['id', 'name']));
    }

    /**
     * 获取市下所有的区县
     * @return \Illuminate\Http\JsonResponse
     */
    public function counties()
    {
        $id = $this->request->input('city_id');

        return $this->response($this->city->counties($id, ['id', 'name']));
    }
}
